/*
 * @lc app=leetcode.cn id=290 lang=javascript
 *
 * [290] 单词规律
 */

// @lc code=start
/**
 * @param {string} pattern
 * @param {string} s
 * @return {boolean}
 */
var wordPattern = function (pattern, s) {
  const words = s.split(" ");
  if (pattern.length !== words.length) return false;
  const wToch = new Map();
  const chTow = new Map();
  let ch, word;
  for (let i = 0; i < pattern.length; i++) {
    ch = pattern[i];
    word = words[i];

    if (wToch.has(word) && wToch.get(word) != ch) return false;
    if (chTow.has(ch) && chTow.get(ch) != word) return false;
    wToch.set(word, ch);
    chTow.set(ch, word);
  }
  return true;
};
// @lc code=end
